<?php

namespace app\health\job;

use app\health\model\HealthFuluOrder;
use app\health\model\HealthFuluOrderItem;
use app\health\service\FuluOpenService;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\queue\Job;

class FuluJob extends BaseJob
{
    /**
     * 福禄直充订单
     * @param Job $job
     * @param $data
     * @return void
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function directCharge(Job $job, $data): void
    {
        $job->delete();
        $order = HealthFuluOrder::where('id', $data['order_id'] ?? 0)->find();
        if ($order && $order['pay_status'] === 1 && $order['order_status'] !== 1) {
            $fuluService = new FuluOpenService();
            $orderItem   = $order->orderItem()->where('status', '0')->select();
            foreach ($orderItem as $item) {
                $result = $fuluService->orderDirect($item['customer_order_no'], $item['goods_code'], $item['account']);
                if ($result) {
                    //下单成功,发起查询订单状态队列
                    queue('\app\health\job\FuluJob@orderStatusSync', [
                        'customer_order_no' => $item['customer_order_no']
                    ], 3, 'health');
                }
            }
        }
    }

    /**
     * 同步福禄订单充值状态
     * @param Job $job
     * @param $data
     * @return true
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function orderStatusSync(Job $job, $data): bool
    {
        if ($job->attempts() >= 20) {
            $job->delete();
        }
        $orderItem = HealthFuluOrderItem::where(['customer_order_no' => $data['customer_order_no'] ?? ''])->find();
        if ($orderItem && $orderItem['status'] === 0) {
            $fuluService = new FuluOpenService();
            //查询福禄直充订单
            $state = $fuluService->orderInfoGet($orderItem['customer_order_no']);
            if ($state === 'success') {
                //充值成功
                $orderItem->status = 1;
                $orderItem->save();
            } else if ($state === 'failed') {
                //充值失败
                $orderItem->save(['status' => 2]);
            } else if ($state === 'untreated' || $state === 'processing') {
                //未处理和处理中重发
                $job->delete();
                $job->release(5);
                return true;
            }
        }
        //全部充值成功则更新为定主订单已完成
        if ($orderItem['status'] !== 2) {
            $order   = HealthFuluOrder::where('id', $orderItem['order_id'])->with(['orderItem'])->find();
            $success = true;
            foreach ($order->orderItem as $item) {
                if ($item['status'] !== 1) {
                    $success = false;
                }
            }
            $success && $order->save(['order_status' => 1, 'updated_at' => date('Y-m-d H:i:s')]);
        }
        $job->delete();
        return true;
    }
}